package com.strongwell.awe.controller;

import com.alibaba.fastjson.JSON;
import com.strongwell.awe.model.CmsArticleEntity;
import com.strongwell.awe.model.CmsCategoryEntity;
import com.strongwell.awe.model.TbAdmin;
import com.strongwell.awe.service.CmsArticleService;
import com.strongwell.awe.service.CmsCategoryService;
import com.strongwell.awe.service.MainService;
import com.strongwell.awe.util.Query;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("web/")
public class SwWebController {
    @Autowired
    private CmsCategoryService cmsCategoryServiceImpl;
    @Autowired
    private CmsArticleService cmsArticleServiceImpl;

    @RequestMapping("/index")
    public String main(HttpServletRequest req) {
        List<CmsCategoryEntity> cmsCategoryEntity = cmsCategoryServiceImpl.queryNameList();
        List<CmsCategoryEntity> Categorylist = cmsCategoryServiceImpl.queryCategoryList();
        List<CmsArticleEntity> articlelist = cmsArticleServiceImpl.queryArticleList(3);

        req.setAttribute("category",cmsCategoryEntity);
        req.setAttribute("catlist",Categorylist);
        req.setAttribute("articlelist",articlelist);


        return "SWweb/index";
    }

    @RequestMapping("/list/{module}")
    public String list(HttpServletRequest req,@PathVariable("module") String module,Map<String, Object> map) {
        //导航菜单
        List<CmsCategoryEntity> cmsCategoryEntity = cmsCategoryServiceImpl.queryNameList();
        List<CmsArticleEntity> cmsArticles = cmsArticleServiceImpl.queryArticle(module);
        System.out.println(JSON.toJSONString(cmsArticles));

        req.setAttribute("category",cmsCategoryEntity);
        if (cmsArticles.size()>0){
            req.setAttribute("article",cmsArticles);
            req.setAttribute("articlestr",JSON.toJSONString(cmsArticles));
        }

        req.setAttribute("module",module);
        return "SWweb/"+module;
    }
    @RequestMapping("/articlelist")
    public String articlelist(HttpServletRequest req,@RequestParam Map<String, Object> params) {
        //导航菜单
        List<CmsCategoryEntity> cmsCategoryEntity = cmsCategoryServiceImpl.queryNameList();
        List<CmsCategoryEntity> Categorylist = cmsCategoryServiceImpl.queryCategoryList();
        req.setAttribute("category",cmsCategoryEntity);
        req.setAttribute("catlist",Categorylist);

        Query query = new Query(params);
        List<CmsArticleEntity> articleList = cmsArticleServiceImpl.queryList(query);
        int total = cmsArticleServiceImpl.queryTotal(query);
        req.setAttribute("articlelist",articleList);
        req.setAttribute("total",total);
        return "SWweb/news";
    }
}
